import { createSlice } from "@reduxjs/toolkit";

const counterStore = createSlice({
  name: "counter",
  // 初始化 state
  initialState: {
    count: 0,
  },
  // 修改数据的方法,同步方法
  reducers: {
    increment: (state) => {
      state.count += 1;
    },
    decrement: (state) => {
      state.count -= 1;
    },
    addToNum: (state, action) => {
      state.count += action.payload;
    },
  },
});

const { increment, decrement, addToNum } = counterStore.actions;

const reducer = counterStore.reducer;

// 以按需导出的方式导出 actionCreater
export { increment, decrement, addToNum };

//以默认导出的方式导出 reducer
export default reducer;
